package com.example.coolshark.controller;

import com.example.coolshark.mapper.BannerMapper;
import com.example.coolshark.mapper.CategoryMapper;
import com.example.coolshark.pojo.entity.Banner;
import com.example.coolshark.pojo.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

/**
 * @Name: CategoryController
 * @Author: 西风残叶
 * @Company: 朝兮网络工作室
 * @Data: 2023/11/21 19:20
 */
@RestController
public class BannerController {
    @Autowired
    BannerMapper mapper;
    @RequestMapping("/banner/select")
    public List<Banner> select(){
        return mapper.select();
    }

    @Value("${dirPath}")
    private String dirPath;
    @RequestMapping("/banner/delete")
    public void delete(int id){
        String url = mapper.selectUrlById(id);
        new File(dirPath+"/"+url).delete();
        mapper.deleteById(id);
    };

    @RequestMapping("/banner/insert")
    public void insert(@RequestBody Banner banner){
        mapper.insert(banner);
    }

    @RequestMapping("/banner/selectById")
    public String selectById(int id){
        return mapper.selectUrlById(id);
    }

    /**
     * 更新横幅信息
     *
     * @param banner 包含更新信息的横幅对象，通过请求体传入
     * @return void 方法没有返回值
     */
    @RequestMapping("/banner/update")
    public void update(@RequestBody Banner banner){
        mapper.update(banner); // 调用mapper层，执行横幅信息的更新操作
    }
}